
/**
 * name:        个人中心--售后服务
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   bgemall.com
 */

$(function(){
    // 个人中心首页 

    if($.cookie('user')!=''&&$.cookie('user')!=null){
        $('.service_wrap').append('<div class="none" style="width:100%;height:300px;background: #fff url(../../img/base/loading.gif) center center no-repeat;"></div>');
        var token='' 
        isLogin();    
        getToken();     // 获取/验证token 
        var url=window.location.href;
        var url_new=url.split('?')[0]
        var pageNo = url.substring(url.lastIndexOf('=')+1, url.length); 
        // 下一页
        if(pageNo<='1'){
            $('.prev').removeClass('prev').addClass('prev1') 
        }else{
            $('.prev1').removeClass('prev1').addClass('prev')
        }
        if($.cookie('serviceIndexpageNo')==pageNo){
            $('.next').removeClass('next').addClass('next1') 
        }else{
            $('.next1').removeClass('next1').addClass('next') 
        }
        $('.page .page_num li.active a').text(pageNo);
        var page_num=parseInt(pageNo);
        $('.page .page_num li:last-child a').text(page_num+1);
        if(pageNo>=$.cookie('serviceIndexpageNo')){
            var page_num=parseInt(pageNo);
            $('.page .page_num li.active a').text(pageNo-1)  ;  
            $('.page .page_num li.active').removeClass('active') ;     
            $('.page .page_num li:last-child a').text(page_num)
            $('.page .page_num li:last-child').addClass('active')
        }
        $('.next').click(function(){
            pageNo++ 
            if($.cookie('serviceIndexpageNo')>=pageNo){
                $('.next').removeClass('next').addClass('next1')
                window.location.href=url_new+'?pageNo='+pageNo     
            }
        });
        $('.next1').click(function(){
            tc('已经是最后一页了')
        });
        //返回首页
        $('.prev_first').click(function(){
            window.location.href=url_new+'?pageNo='+1
        })
        // 上一页
        $('.prev').click(function(){
            pageNo--
            if(pageNo>='1'){
                window.location.href=url_new+'?pageNo='+pageNo     
            }else{
                $('.prev').removeClass('prev').addClass('prev1')
            }
        });
        $('.prev1').click(function(){
            tc('大神们这是首页不要再点了~~~')
        });
        //点击数字
        $('.page .page_num li').click(function(){
            var text=$(this).text()
            pageNo=text
            window.location.href=url_new+'?pageNo='+pageNo 
        })
        if( token!='' && token!=null){
            var refundType = 0;
            getRefundOrderList(token,URL,refundType,pageNo); 
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2');
        var nav='';
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>';
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>';
        nav+='<i class="icon_down"></i>';
        nav+='<ul class="logined">';
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>';
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>';
        nav+='</ul>';
        $('.mod_login').append(nav); 
    } 

    // 获取/验证token 
    function getToken(){ 
        strs=$.cookie('user').split(","); 
        for(var i in strs){  
            $('.nike_name').html(strs[1]);  
            token=strs[6]; 
            name=strs[1]; 
        } 
    } 
    // 退出登录 
    $('.login_out').click(function(){ 
        $.cookie('user', null, { path: '/' }); 
            $.ajax({ 
                type:'post',
                url:new_url+'shop/login/logOut', 
                dataType:'json', 
                data:'', 
                success:function(data){ 
                    tc(data.message); 
                }  
            }); 
            setTimeout(function(){ 
                window.location.href='/index.html'; 
            },1100); 
    }); 

    // 搜索 
    $(".service_search_btn").click(function(){ 
        var searchType = $('#searchType').val();
        var searchContent = $(".search_order_input").val();
        var addTimeFrom = $(".time_from_input").val();
        var addTimeTo = $(".time_end_input").val();
        var refundType;
        var pageNo;
        var pageSize;
        if(searchContent == ''){
            tc('请填写'+$('#searchType').find("option:selected").text());
        }else{
            $('.service_list div, .service_list_box').remove();
            getRefundOrderList(token,URL,refundType,pageNo,5,searchType,searchContent,addTimeFrom,addTimeTo);
        }
    });
    // 全部售后维权 
    $("#service_all").click(function(){ 
        var refundType = 0;
        $(this).addClass('active');
        $("#service_one, #service_two").removeClass('active')
        $('.service_list div, .service_list_box').remove();
        getRefundOrderList(token,URL,refundType);
    });
    // 仅退款 
    $("#service_one").click(function(){ 
        var refundType = 1;
        $(this).addClass('active');
        $("#service_all, #service_two").removeClass('active');
        $('.service_list div, .service_list_box').remove();
        getRefundOrderList(token,URL,refundType);
    });
    // 退款/退货 
    $("#service_two").click(function(){ 
        var refundType = 2;
        $(this).addClass('active');
        $("#service_all, #service_one").removeClass('active');
        $('.service_list div, .service_list_box').remove();
        getRefundOrderList(token,URL,refundType);
    });

});
// 获取售后订单列表
function getRefundOrderList(token,URL,refundType,pageNo,pageSize,searchType,searchContent,addTimeFrom,addTimeTo){
    var data={
        token:token,
        searchType:searchType,
        searchContent:searchContent,
        addTimeFrom:addTimeFrom,
        addTimeTo:addTimeTo,
        refundType:refundType,
        pageNo:pageNo,
        pageSize:pageSize
    }
    $.ajax({
        type:'post',
        url:new_url+'shop/member_refund/getRefundOrderList',
        dataType:'json',
        data:data,  
        success:function(e){ 
            if(e.code=='0'){
                $('.none').hide();
                var pageNo= e.pageInfo.totalPages;
                if(pageNo<='1'){
                    $('.page_num li').eq(0).hide()
                    $('.page_num li').eq(1).hide()
                } 
                $.cookie('serviceIndexpageNo',pageNo,{path:'/'});
                $("#service_all").text('全部售后（'+ e.data.statisticsInfo.totalNum +'）');
                $("#service_one").text('仅退款（'+ e.data.statisticsInfo.refundNum +'）');
                $("#service_two").text('退款/退货（'+ e.data.statisticsInfo.returnGoodsNum +'）');
                if(e.data.orderList != null && e.data.orderList != ''){
                    // console.info(e.data.orderList.length)
                    var serviceListTpl = $("#serviceListTpl").html();
                    for(var i=0; i < e.data.orderList.length; i++){
                        for(var j=0; j < e.data.orderList[i].goodsList.length; j++){
                            var time = e.data.orderList[i].createTime
                            function getLocalTime(nS) {     
                                return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, " ").replace(/日/g, " ");  
                            }

                            var stateDesc = e.data.orderList[i].goodsList[j].refundState;
                            var stateDesc1 = e.data.orderList[i].goodsList[j].stateDesc;
                            var stateDescUrl = '';
                            //售后状态 1.待审核;2.审核不通过;3.审核通过; 4.等待买家发货;
                            //5.买家已发货;6.卖家未收到货; 7.卖家已收货; 8.退款完成;9.退款关闭.（仅退款走到第3步就结束了）

                            // 1.待审核;2.审核不通过;8.退款关闭
                            if(stateDesc == '1' || stateDesc == '2' || stateDesc == '9'){
                                stateDescUrl = 'member_service_two.html?refundId='+e.data.orderList[i].refundId+'&goodsId='+e.data.orderList[i].goodsList[j].goodsId;
                            }
                            // 3.审核通过;4.等待买家发货;5.买家已发货;6.卖家未收到货;
                            if(stateDesc == '3' || stateDesc == '4' || stateDesc == '5' || stateDesc == '6' || stateDesc == '7'){
                                stateDescUrl = 'member_service_three.html?refundId='+e.data.orderList[i].refundId+'&goodsId='+e.data.orderList[i].goodsList[j].goodsId;
                            }
                            // 7.已完成
                            if(stateDesc == '8'){
                                stateDescUrl = 'member_service_four.html?refundId='+e.data.orderList[i].refundId+'&goodsId='+e.data.orderList[i].goodsList[j].goodsId;
                            }

                            if(e.data.orderList[i].goodsList[j].spec == null){
                                e.data.orderList[i].goodsList[j].spec = '默认'
                            }
                            var serviceListCont=template(
                                serviceListTpl, {
                                    serviceList: [{
                                        refundId:e.data.orderList[i].refundId,      // 售后ID
                                        orderId:e.data.orderList[i].orderId,      // 订单ID
                                        orderNo:e.data.orderList[i].orderNo,      // 订单号
                                        createTime:getLocalTime(time),      // 申请时间
                                        refundNo:e.data.orderList[i].refundNo,      // 售后单号
                                        goodsId:e.data.orderList[i].goodsList[j].goodsId,
                                        image:e.data.orderList[i].goodsList[j].image,
                                        name:e.data.orderList[i].goodsList[j].name,
                                        spec:e.data.orderList[i].goodsList[j].spec,
                                        unitPrice:e.data.orderList[i].goodsList[j].unitPrice,
                                        num:e.data.orderList[i].goodsList[j].num,
                                        transactionPrice:e.data.orderList[i].goodsList[j].transactionPrice,
                                        refundPrice:e.data.orderList[i].goodsList[j].refundPrice,
                                        refundState:e.data.orderList[i].goodsList[j].refundState,
                                        stateDesc:stateDesc1,
                                        stateDescUrl:stateDescUrl,
                                    }]
                                }
                            );
                            $('.service_list').append(serviceListCont);
                            $('.page').show()
                            $("#timeFrom, #timeEnd").on("click",function(e){
                                e.stopPropagation();
                                $(this).lqdatetimepicker({
                                    css : 'datetime-day',
                                    dateType : 'D',
                                    selectback : function(){}
                                });
                            });
                        }
                    }
                }else{
                    $(".page").remove();
                    $('.service_list').append('<div style="width:100%;height:200px;line-height:200px;text-align:center;color:#999;">暂无售后维权信息</div>');
                }
                
            }else{
                tc(e.message);
                setTimeout(function(){
                        window.location.href='/src/login/login.html?type=2';
                },1100)
            } 
        }
    });
}